package com.renwei.yunlong.fragment;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.work.PhotoDisplayActivity;
import com.renwei.yunlong.adapter.NewDetailsAnnexAdapter;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.WorkAnnexBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.renwei.yunlong.http.Api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;


/**
 * 对象详情  附件  页面
 * <p>
 * Created by Administrator on 2017/9/7.
 */

public class ObjectAnnexFragment extends BaseLazyFragment implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.recyclerView)
    XRecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;

    private NewDetailsAnnexAdapter adapter;
    private int page = 1;
    private String outSourceFlag;
    private String hardwareId;

    @SuppressLint("ValidFragment")
    public ObjectAnnexFragment(String outSourceFlag, String hardwareId) {
        this.hardwareId = hardwareId;
        this.outSourceFlag = outSourceFlag;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_new_details_annex;
    }

    @Override
    public void onLazyLoad() {
        initView();
        initData();
    }

    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                recyclerView.setLoadingMoreEnabled(false);
                adapter.clean();
                initData();
            }

            @Override
            public void onLoadMore() {
                page++;
                initData();
            }
        });
        stateLayout.setRefreshListener(new StateLayout.OnViewRefreshListener() {
            @Override
            public void refreshClick() {
                page = 1;
                recyclerView.setLoadingMoreEnabled(false);
                adapter.clean();
                initData();
            }

            @Override
            public void loginClick() {

            }
        });

        adapter = new NewDetailsAnnexAdapter(getContext());
        adapter.setOnItemClickLitener(new BaseRecyclerViewAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                ArrayList<String> paths = new ArrayList<String>();
                for (WorkAnnexBean.Rows rows : adapter.getData()) {
                    if ("1".equals(companyType)) {
                        paths.add(Api.$().OWNER_FILE_URL + rows.getAttachPath());
                    } else if ("2".equals(companyType)) {
                        paths.add(Api.$().SERVICE_FILE_URL + rows.getAttachPath());
                    }
                }
                PhotoDisplayActivity.OpenActivity(getContext(), paths, position);
            }
        });
        recyclerView.setAdapter(adapter);
    }

    public void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("correlationId", hardwareId);
        map.put("type", "3");
        map.put("page", page + "");
        map.put("rows", "20");
        ServiceRequestManager.getManager().getObjectAnnex(getContext(),
                outSourceFlag, JsonMapUtil.mapToJson(map), this);

    }

    //数据源
    private void setDataList(List<WorkAnnexBean.Rows> mList) {
        adapter.addAll(mList);
        if (mList.size() < 20) {
            recyclerView.setLoadingMoreEnabled(false);
        }
        if (adapter.getData().size() == 0) {
            stateLayout.showEmptyView();
        } else {
            stateLayout.showContentView();
        }

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onSuccess(int requestId, String result) {
        recyclerView.loadMoreComplete();
        recyclerView.refreshComplete();
        WorkAnnexBean ownerBean = new Gson().fromJson(result, WorkAnnexBean.class);
        if (ownerBean.getMessage().getCode() == 200) {
            setDataList(ownerBean.getRows());
        }
    }

    @Override
    public void onException(int requestId, String code) {
        page--;
        recyclerView.loadMoreComplete();
        recyclerView.refreshComplete();
        stateLayout.showErrorView();
    }
}
